package presentation;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class LoginForm extends javax.swing.JDialog {

	private static final long serialVersionUID = 3487576749815702846L;

	private JLabel nameLabel;
	private JLabel pwdLabel;
	private JPasswordField passwordField;
	private JButton cancelButton;
	private JButton loginButton;
	private JTextField nameField;

	public LoginForm(JFrame frame) {
		super(frame);
		this.getParent().setEnabled(false);
		initGUI();
	}

	private void initGUI() {
		try {
			{
				AnchorLayout thisLayout = new AnchorLayout();
				this.setTitle("Log In");
				getContentPane().setLayout(thisLayout);
				this.setPreferredSize(new java.awt.Dimension(299, 170));
				this.setAlwaysOnTop(true);
				this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
				this.addWindowListener(closeHandler(this));
				this.setResizable(false);
				{
					cancelButton = new JButton();
					getContentPane().add(cancelButton, new AnchorConstraint(628, 823, 863, 517, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					cancelButton.setText("Cancel");
					cancelButton.setPreferredSize(new java.awt.Dimension(89, 32));
					cancelButton.setFont(new java.awt.Font("Aharoni",0,16));
					cancelButton.addActionListener(cancelHandler(this));
				}
				{
					loginButton = new JButton();
					getContentPane().add(loginButton, new AnchorConstraint(628, 458, 863, 152, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					loginButton.setText("Log In");
					loginButton.setPreferredSize(new java.awt.Dimension(89, 32));
					loginButton.setFont(new java.awt.Font("Aharoni",0,16));
					this.loginButton.addActionListener(loginHandler(this));
				}
				{
					passwordField = new JPasswordField();
					getContentPane().add(passwordField, new AnchorConstraint(334, 908, 488, 328, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					passwordField.setPreferredSize(new java.awt.Dimension(169, 21));
				}
				{
					nameField = new JTextField();
					getContentPane().add(nameField, new AnchorConstraint(106, 915, 261, 324, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					nameField.setPreferredSize(new java.awt.Dimension(172, 21));
				}
				{
					pwdLabel = new JLabel();
					@SuppressWarnings("unused")
					AnchorLayout pwdLabelLayout = new AnchorLayout();
					getContentPane().add(pwdLabel, new AnchorConstraint(341, 348, 444, 32, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					pwdLabel.setText("Password :");
					pwdLabel.setPreferredSize(new java.awt.Dimension(92, 14));
					pwdLabel.setLayout(null);
					pwdLabel.setFont(new java.awt.Font("Tahoma",0,16));
				}
				{
					nameLabel = new JLabel();
					@SuppressWarnings("unused")
					AnchorLayout nameLabelLayout = new AnchorLayout();
					getContentPane().add(nameLabel, new AnchorConstraint(84, 262, 253, 32, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					nameLabel.setText("Name : ");
					nameLabel.setPreferredSize(new java.awt.Dimension(67, 23));
					nameLabel.setLayout(null);
					nameLabel.setFont(new java.awt.Font("Tahoma",0,16));
				}
			}
			pack();
			this.setSize(299, 170);
			this.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	/**
	 * enable my owner (meening - the JFrame that is passed to the Constructor)
	 * and close myself
	 */
	private void close() {
		this.getParent().setEnabled(true);
		this.dispose();
	}


	/**
	 * return an Action Listener. this Listener gets this LoginForm
	 * and invoke its close() method
	 * @param me - the LoginForm where the button is in
	 * @return an action listener
	 */
	private ActionListener cancelHandler(final LoginForm me) {
		return new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				me.close();
			}
		};
	}

	/**
	 * return an Action Listener. this Listener gets this LoginForm
	 * and invoke its close() method
	 * @param me - the LoginForm where the button is in
	 * @return an action listener
	 */
	private ActionListener loginHandler(final LoginForm me) {
		return new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				WsepGUI tGUI = (WsepGUI)me.getParent();
				String name = me.nameField.getText();
				String pwd = new String(me.passwordField.getPassword());
				tGUI.login(me, name, pwd);
				me.close();
			}
		};
	}



	/**
	 * return a Window Listener. this Listener gets this LoginForm
	 * only the WindowClosed() method is implemented and it invoke me.close()
	 * @param me - the LoginForm where the button is in
	 * @return a Window listener
	 */
	private WindowListener closeHandler(final LoginForm me) {
		return new WindowListener() {
			public void windowActivated(WindowEvent e) {
				// TODO Auto-generated method stub

			}
			public void windowClosed(WindowEvent e) {
				me.close();
			}
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub

			}
			public void windowDeactivated(WindowEvent e) {
				// TODO Auto-generated method stub

			}
			public void windowDeiconified(WindowEvent e) {
				// TODO Auto-generated method stub

			}
			public void windowIconified(WindowEvent e) {
				// TODO Auto-generated method stub

			}
			public void windowOpened(WindowEvent e) {
				// TODO Auto-generated method stub

			}
		};
	}

}
